//声明一个域
var app = angular.module("addApp", []);
//向后台传输数据格式设置
app.config(function($httpProvider) {
			$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
			$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
//			$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
			$httpProvider.transformRequest=function(obj) {    
                var str = [];    
                for (var p in obj) {    
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));    
                }    
                return str.join("&");    
            }  
		});

app.controller('addCtrl', function($scope, $http, $window, $timeout) {
	var avoidDup = true;
	// 点击确定方法
	$scope.addWeibo = function() {
		if(avoidDup==false){
			return false;
		}
		var ids = $(".editExaDataBtn").attr("ids");
		var vm = $scope.vm = {
//			"tagvalue_id" : $scope.tagvalue_id,
			"microuser_bloggerId" : $scope.microuser_bloggerId,
			"microuser_blogger" : $scope.microuser_blogger,
			"microuser_bloggerUrl" : $scope.microuser_bloggerUrl,
			tagvalue_id : ids,
		};

		if(vm.microuser_bloggerId!=null && vm.microuser_blogger!=null&&vm.microuser_bloggerUrl!=null){
//			//验证url地址
//			var strReg = "^((https|http|ftp|rtsp|mms)?://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?";
//			var re = new RegExp(strReg);
//			if(!re.test($.trim(vm.microuser_bloggerUrl))) {
//				layer.msg("请输入有效的博主URL地址",2,-1);
//				return false;
//			}
			avoidDup =false;
			$.post(basePath + 'microuser/addMicrouser.do', vm).success(function(data) {
				if (data.returnCode == 10000) {
					layer.msg(data.returnMessage, 1, 10);
					$timeout(function(){
						var index = parent.layer.getFrameIndex(window.name);
						try{
							window.parent.$('#Msearch').trigger('click');
						}catch(e){}
						parent.layer.close(index);
					},1000);
				} else if (data.returnCode == 999994) {
					setUserTimeout(data);
				} else {
					layer.msg(data.returnMessage, 2, -1);
					avoidDup =true;
					return false;
				}
			});
		}
	}

	//取消关闭弹窗
	$scope.close = function(e){
		 window.parent.layer.closeAll();
	}
});

//open edit Layer
var editExaDataLayer = null;
var ruleSelectType = 0;
$('.editExaDataBtn').live('click', function() {
    var editExaDataLayer = $.layer({
        type: 1,
        title: false,
        area: ['700', 'auto'],
        border: [0], //去掉默认边框
        shade: [0.3, '#000'], //去掉遮罩
        closeBtn: [0, false], //去掉默认关闭按钮
        fadeIn: 500,
        move: "#editExaData_tile",
        page: {
            dom: '#editExaDataLayer'
        }
    });
    //自设关闭
    $('#editExaDataLayer > .closeBtn,#editExaDataLayer .cancelLayer,#editExaSubmitBtn').live('click', function() {
        layer.close(editExaDataLayer);
    });
});

var edit = {
	    "qCompanyList": function(_this, weiboArr, jsons) {
	        var _obj = $("#CustomsAreaList");
	        $.ajax({
	            type: "POST",
	            url: basePath + "Tagvalue/queryTagvalue.do",
	            data: jsons,
	            success: function(data) {
	                if(data.returnCode == 10000) {
	                    var s2 = data.returnData.length;
	                    if(s2 > 0) {
	                        var htmls = "";
	                        var checkedHtmls = "";
	                        for(var i = 0; i < s2; i++) {
	                            var checkedBtn = "";
	                            var checked = "";
	                            var tagvalue_id = data.returnData[i].tagvalue_id;
	                            var checkeddefault = "";
	                            var num = weiboArr.length;
	                            var tf = null;
	                            for(var nn = 0; nn < num; nn++) {
	                                if(tagvalue_id == weiboArr[nn]) {
	                                    checkedBtn = "true";
	                                    checked = "checked";
	                                    checkeddefault = "true";
	                                    tf = true;
	                                    break;
	                                } else {
	                                    checkedBtn = "false";
	                                    checkeddefault = "false";
	                                    checked = "";
	                                    tf = false;
	                                }
	                            }
	                            if(tf == true) {
	                                checkedHtmls += "<div class='infoLlist' title='" + data.returnData[i].tag_value + "'><span class='ui_checkbox " + checked + "' tagv-id= '" + data.returnData[i].tagvalue_id + "' checkedBtn='" + checkedBtn + "' checkeddefault='" + checkeddefault + "'></span> <font>" + data.returnData[i].tag_type +"-"+ data.returnData[i].tag_value + "</font></div>";
	                            } else if(tf == false) {
	                                htmls += "<div class='infoLlist' title='" + data.returnData[i].tag_value + "'><span class='ui_checkbox " + checked + "' tagv-id= '" + data.returnData[i].tagvalue_id + "' checkedBtn='" + checkedBtn + "' checkeddefault='" + checkeddefault + "'></span> <font>" + data.returnData[i].tag_type +"-"+ data.returnData[i].tag_value + "</font></div>";

	                            }
	                        }
	                        _obj.html(checkedHtmls + htmls + "<div class='clear'></div>");
	                        checkTrueFalse();
	                    }
	                    if(s2 < 1) {
	                        _obj.html("<br><bR><p align='center'>很遗憾，没有查询到相关的数据！</p><br><bR>");
	                    }
	                } else if(data.returnCode == 999994) {
	                    setUserTimeout(data);
	                } else {
	                    layer.msg(data.returnMessage, 2, -1);
	                }
	            },
	            error: function(message) {
	                ayer.msg("操作失败，请重试！", 2, -1);
	            },
	            beforeSend: function() {
	                var imgGif = "<p class='loadingGif'></p>";
	                _obj.html(imgGif);
	            }
	        });
	    },
	    "quyeryCheck": function(ids, names) {
	        var $selectShowSpan = $(".editExaDataBtn");
	        var $showSpan = $selectShowSpan.parent().prev("span");
	        $selectShowSpan.attr({
	            ids: ids
	        });
	        $showSpan.attr({
	            ids: ids
	        });
	        $showSpan.text(names);
	    }
	}
	$(".infoLlist .ui_checkbox").live("click", function() {
	    if($(this).attr("checkedBtn") == "true") {
	        $(this).attr("checkedBtn", "false");
	        $(this).removeClass("checked");
	    } else {
	        $(this).attr("checkedBtn", "true");
	        $(this).addClass("checked");
	    }
	});

	$(".editExaDataBtn").live("mouseover", function() {
	    var strId = $(this).attr('ids');
	    var weiboArr = strId.split(",");
	    ruleSelectType = $(this).attr('type');
	    var params = {
	        relation_type: ruleSelectType
	    };
	    edit.qCompanyList($(this), weiboArr, params);
	});

	$("#tagvalueQueryBtn").live("click", function() {
	    publicQDataList3($("#tagvName"));
	});
	$('.enterKeyDownElement2').keydown(function(e) {
	    if(e.keyCode == 13) {
	        publicQDataList3($("#tagvName"));
	    }
	});

	function publicQDataList3(_this) {
	    var kV = $.trim(_this.val());
	    $("#CustomsAreaList .infoLlist").each(function() {
	        var name = $(this).children("font").text();
	        var isOf = $.trim(name.indexOf(kV));
	        if(isOf > -1) {
	            $(this).show();
	        } else {
	            $(this).hide();
	        }
	    });

	}

	$("#editExaSubmitBtn").live("click", function() {
	    var trueArr = new Array();
	    var trueStrArr = new Array();
	    $("#CustomsAreaList .ui_checkbox").each(function() {
	        var id = $(this).attr("tagv-id");
	        var name = $(this).next("font").text();
	        var checkedbtn = $(this).attr("checkedbtn");
	        var checkeddefault = $(this).attr("checkeddefault");
	        if(checkedbtn == "true") {
	            trueArr.push(id);
	            trueStrArr.push(name);
	        };
	    });
	    var trueArrs = "";
	    var trueStrArrs = "";
	    if(trueArr.length > 0) {
	        trueArrs = trueArr.join(",");
	        trueStrArrs = trueStrArr.join(",");
	    }
	    edit.quyeryCheck(trueArrs, trueStrArrs);
	    $(".xubox_main").css("cssText", "height:45px!important; z-index:198916;");
	});

	$(".selectAllAreaGoGo .allSel").live("click", function() {
	    var checkedbtn = $(this).attr("checkedbtn");
	    if(checkedbtn == "true") {
	        $("#CustomsAreaList .infoLlist .ui_checkbox").addClass("checked").attr("checkedbtn", "true");
	    } else {
	        $("#CustomsAreaList .infoLlist .ui_checkbox").removeClass("checked").attr("checkedbtn", "false");
	    }
	});

	$(".selectAllAreaGoGo .reDefault").live("click", function() {
	    $("#CustomsAreaList .infoLlist .ui_checkbox").each(function() {
	        var checkeddefault = $(this).attr("checkeddefault");
	        if(checkeddefault == "true") {
	            $(this).addClass("checked").attr("checkedbtn", "true");
	        } else {
	            $(this).removeClass("checked").attr("checkedbtn", "false");
	        }
	    });
	    checkTrueFalse();
	});

	$("#CustomsAreaList .infoLlist .ui_checkbox").live("click", function() {
	    checkTrueFalse();
	});

	function checkTrueFalse() {
	    var isTrue = "true";
	    $("#CustomsAreaList .infoLlist .ui_checkbox").each(function() {
	        var checkedbtn = $(this).attr("checkedbtn");
	        if(checkedbtn == "false") {
	            isTrue = "false";
	            return;
	        }
	    });
	    if(isTrue == "false") {
	        $(".selectAllAreaGoGo .allSel").removeClass("checked").attr("checkedbtn", "false");
	    } else {
	        $(".selectAllAreaGoGo .allSel").addClass("checked").attr("checkedbtn", "true");
	    }
	}
